<?php
	if(!defined("IN_APP"))
	{
		exit;
	}
	
	if(!adminpermission('C','C100001'))
	{
		echo '无权查看!';
		exit;
	}
	
	$method = getParam('m','get','list');
	
	$parm = requestArrayParm($_REQUEST);
	
	if($method == 'addCPitem')
	{
		$pagetitle = '创建项目信息';
		
		if($parm['pid'])
		{
			unset($where,$data);
			$where['id'] = $parm['pid'];
			$info = _ChanPin::getChanPinInfo($where);
			if($info)
			{
				include renderTemplate('admin/chanpin/item');
			}
			else
			{
				header("Location:".SITE_URL.'admin.php?a=chanpinM');
			}
		}
		else
		{
			header("Location:".SITE_URL.'admin.php?a=chanpinM');
		}
	}
	
	if($method == 'editCPitem')
	{
		$pagetitle = '编辑项目信息';
		
		if($parm['id'])
		{
			unset($where,$data);
			$where['id'] = $parm['id'];
			$item = _ChanPin::getChanPin_ItemInfo($where);
			if($item)
			{
				unset($where,$data);
				$where['id'] = $item['pid'];
				$info = _ChanPin::getChanPinInfo($where);
				include renderTemplate('admin/chanpin/item');
			}
			else
			{
				header("Location:".SITE_URL.'admin.php?a=chanpinM');
			}
		}
		else
		{
			header("Location:".SITE_URL.'admin.php?a=chanpinM');
		}
	}
	
	if($method == 'list')
	{
		unset($wheres,$pages,$sort);
		
		$wheres = array();
		if($parm['submit'])
		{
			if($parm['keyword'])
			{
				$wheres['name'] = $parm['keyword'];
			}
			
			if($parm['type'])
			{
				$wheres['status'] = $parm['type'];
			}
		}
		
		
		
		$page = $pages['page'] = getParam('page','request','1');
		$pages['rows'] = 20;
		$sort['id'] = 'desc';
		$resData = _ChanPin::getChanPinList($wheres, $pages,$sort);
		
		foreach ($resData['list']  as $key => $value)
		{
			$value['opinfo'] = _OpLog::getLastOpLog(array('table'=>'chanpin','union_id'=>$value['id']));
			$value['items'] = _ChanPin::getChanPin_ItemCount(array('pid'=>$value['id'],'status'=>'1'));		
			$list[] = $value;
		}
		
		$pageurl = SITE_URL.'admin.php?'.set_URL_Param(get_URL_Param());
	
		$pagetotal = $resData["pages"];
		
		include renderTemplate('admin/chanpin/list');
	}
	
	if($method == 'addCP')
	{
		$pagetitle = '创建产品信息';
		
		include renderTemplate('admin/chanpin/info');
	}

	if($method == 'editCP')
	{
		$pagetitle = '编辑产品信息';
		
		if($parm['id'])
		{
			unset($where,$data);
			$where['id'] = $parm['id'];
			$chanpin = _ChanPin::getChanPinInfo($where);
			if($chanpin)
			{
				
			}
			else
			{
				header("Location:".SITE_URL.'admin.php?a=chanpinM');
			}
		}
		else
		{
			header("Location:".SITE_URL.'admin.php?a=chanpinM');
		}
		
		include renderTemplate('admin/chanpin/info');
	}

	
	if($method == 'itemlist')
	{
		if($parm['pid'])
		{
			unset($where,$data);
			$where['id'] = $parm['pid'];
			$chanpin = _ChanPin::getChanPinInfo($where);
			if($chanpin)
			{
				unset($wheres,$data);
				
				$wheres = array();
				
				$wheres['pid'] = $chanpin['id'];
				
				if($parm['keyword'])
				{
					$wheres['name'] = $parm['keyword'];
				}
				
				$page = $pages['page'] = getParam('page','request','1');
				$pages['rows'] = 20;
				$sort['sort'] = 'desc';
				
				$resData = _ChanPin::getChanPin_ItemList($wheres, $pages,$sort);
				
				foreach ($resData['list']  as $key => $value)
				{
					$value['opinfo'] = _OpLog::getLastOpLog(array('table'=>'chanpin_item','union_id'=>$value['id']));
					
					$list[] = $value;
				}
				
				$pageurl = SITE_URL.'admin.php?'.set_URL_Param(get_URL_Param());
			
				$pagetotal = $resData["pages"];
				
				
				include renderTemplate('admin/chanpin/itemlist');
				
				
			}
			else
			{
				//header("Location:".SITE_URL.'admin.php?a=chanpinM');
			}
		}
		else
		{
			//header("Location:".SITE_URL.'admin.php?a=chanpinM');
		}
		
		
		
	}
	
	if($method == 'additem')
	{
		$pagetitle = '创建项目信息';
		
		include renderTemplate('admin/chanpin/item');
	}
	
	if($method == 'edititem')
	{
		$pagetitle = '修改项目信息';
		
		include renderTemplate('admin/chanpin/item');
	}
	
	
	if($method == 'edit')
	{
		$pagetitle = '修改产品';
		
		$id = getParam('id');
		if($id)
		{
			//查询产品信息
			include renderTemplate('admin/chanpin/info');
		}
	}